home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr31 / hades.zip / SORTPWD.C < prev    next >
C/C++ Source or Header  |  1992-04-14  |  8KB  |  330 lines

  1.  
  2. /***************
  3. **
  4. **  sortpwd.c
  5. **  last revised: april 14, 1992
  6. **
  7. **  Sortpwd sorts a password-file on a selected password-field
  8. **  in the selected order.
  9. **
  10. **  Written for DESPERATE password-cracker with HADES encryption engine by
  11. **  Remote.
  12. **
  13. **  Copyright (C)1992, Zabkar
  14. **
  15. **  root@waves.hacktic.nl (Zabkar)
  16. **  root@room101.hacktic.nl (Remote)
  17. **
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <alloc.h>
  23. #include "pwd.h"
  24.  
  25.  
  26. #define NAME       0
  27. #define PASSWORD   1
  28. #define UID        2
  29. #define GID        3
  30. #define GECOS      4
  31. #define HOMEDIR    5
  32. #define SHELL      6
  33.  
  34.  
  35. FILE *infile;
  36. FILE *outfile;
  37. extern FILE *_pw_file;
  38.  
  39.  
  40. struct entries
  41. {
  42.     struct passwd password;
  43.     struct entries *next;
  44. };
  45.  
  46.  
  47.  
  48. /***************
  49.  haltusage()
  50.  prints correct usage and exits
  51. ****************/
  52.  
  53. void haltusage()
  54. {
  55.   fprintf(stderr,
  56.   "Sortpwd version 1.00 alpha, Copyright (C)1992 Zabkar\n"\
  57.   "DESPERATE password-cracker 1.0 alpha using HADES engine by Remote.\n\n"\
  58.   "Usage: sortpwd [-u|-p|-i|-g|-n|-h|-s] [-r] [-f passwordfile] "\
  59.   "[-o outfile]\n\n"\
  60.   "\t-u: sort by username\n"\
  61.   "\t-p: sort by password (salt)\n"\
  62.   "\t-i: sort by user-id\n"\
  63.   "\t-g: sort by group-id\n"\
  64.   "\t-n: sort by real name (gecos-field)\n"\
  65.   "\t-h: sort by home-directory\n"\
  66.   "\t-s: sort by shell\n"\
  67.   "\t-r: reverse sorting order (default is ascending)\n"\
  68.   "\t-f: read from 'passwordfile' instead of stdin\n"\
  69.   "\t-o: write to 'outfile' instead of stdout\n\n"\
  70.   "default field to be sorted on is user-id\n"\
  71.   "no -f specified: dictfile read from stdin\n"\
  72.   "no -o specified: output written to stdout\n");
  73.   exit(0);
  74. }
  75.  
  76.  
  77. /***************
  78.  freebuf()
  79.  freebuf erases all memory occupied by linked-list begin.
  80. ***************/
  81.  
  82. void free_buf(struct entries *begin)
  83. {
  84.     struct entries *p;
  85.  
  86.     if (begin)
  87.     {
  88.       p = begin;
  89.       while (p != NULL)
  90.       {
  91.         p = p->next;
  92.         free(begin);
  93.         begin = p;
  94.       }
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. /***************
  101.  haltmemory()
  102.  displays out-of-memory message and stops execution.
  103. ***************/
  104.  
  105. void haltmemory(struct entries *begin)
  106. {
  107.     free_buf(begin);
  108.  
  109.     fprintf(stderr, "Not enaugh memory to sort entire file\n");
  110.     exit(0);
  111. }
  112.     
  113.  
  114.  
  115. /***************
  116.  getfield()
  117.  returns the field that we are going to sort on in dest. numeric fields
  118.  are presentated right-aligned in a string[6], so that they can be
  119.  strcmp'd too.
  120. ***************/
  121.  
  122. char *get_field(char *dest, struct passwd *p, int field)
  123. {
  124.     switch(field)
  125.     {
  126.         case NAME     : strcpy(dest,p->pw_name); break;
  127.         case PASSWORD : strcpy(dest,p->pw_passwd); break;
  128.         case UID      : sprintf(dest, "%6d", p->pw_uid); break;
  129.         case GID      : sprintf(dest, "%6d", p->pw_gid); break;
  130.         case GECOS    : strcpy(dest,p->pw_gecos); break;
  131.         case HOMEDIR  : strcpy(dest,p->pw_dir); break;
  132.         case SHELL    : strcpy(dest,p->pw_shell); break;
  133.     }
  134.     return (dest);
  135. }
  136.  
  137.  
  138.  
  139. /***************
  140.  sort()
  141.  sorts passwordfile `infile' by field `field' to linked-list begin.
  142.  if `asc'==0 then passwordfile is sorted in descending order, otherwise
  143.  in ascending order.
  144. ***************/
  145.  
  146. void *sort(FILE *infile, struct entries **begin, char field, char asc)
  147. {
  148.     struct entries *p;
  149.     struct entries *q;
  150.     struct entries *r;
  151.     struct passwd *temp;
  152.     char dest1[256], dest2[256];
  153.  
  154.     _pw_file = infile;
  155.  
  156.     if (!(*begin = malloc(sizeof(struct entries))))
  157.        haltmemory(*begin);
  158.  
  159.     memset(&((*begin)->password), 0, sizeof(struct passwd));
  160.     (*begin)->next = NULL;
  161.  
  162.     while ((temp = getpwent()) != NULL)
  163.     {
  164.         if (!(q = malloc(sizeof(struct entries))))
  165.            haltmemory(*begin);
  166.  
  167.         p = *begin;
  168.  
  169.         get_field(dest1, temp, field);
  170.  
  171.         if (asc)
  172.           while ((p->next != NULL) &&
  173.             (strcmp(get_field(dest2, &(p->next->password), field), dest1) < 0))
  174.              p = p->next;
  175.         else
  176.           while ((p->next != NULL) &&
  177.             (strcmp(get_field(dest2, &(p->next->password), field), dest1) > 0))
  178.              p = p->next;
  179.  
  180.         r = p->next;
  181.         p->next = q;
  182.  
  183.         /* We have to copy the entire structure since getpwent uses a
  184.            static structure that is overwritten each call */
  185.         memcpy(&(q->password), temp, sizeof(struct passwd));
  186.  
  187.         q->next = r;
  188.     }
  189. }
  190.  
  191.  
  192.  
  193. /***************
  194.  to_file()
  195.  writes contents of linked-list begin to file of.
  196. ***************/
  197.  
  198. void to_file(FILE *of, struct entries *begin)
  199. {
  200.     struct entries *p;
  201.  
  202.     p = begin->next;
  203.     while (p != NULL)
  204.     {
  205.         fprintf(of, "%s:%s:%d:%d:%s:%s:%s\n",
  206.         p->password.pw_name, p->password.pw_passwd, p->password.pw_uid,
  207.         p->password.pw_gid, p->password.pw_gecos, p->password.pw_dir,
  208.         p->password.pw_shell);
  209.         p = p->next;
  210.     }
  211. }
  212.  
  213.  
  214.  
  215. /***************
  216.  do_sortpwd()
  217.  sorts passwordfile `inf' to file `of' by field. If asc==0 sorting is
  218.  done descending, otherwise ascending.
  219. ***************/
  220.  
  221. void do_sortpwd(FILE *inf, FILE *of, char field, char asc)
  222. {
  223.   struct entries *begin;
  224.  
  225.   sort(inf, &begin, field, asc);
  226.   to_file(of, begin);
  227.   free_buf(begin);
  228. }
  229.  
  230.  
  231. /***************
  232.  main()
  233.  main function of password-sorter.
  234. ***************/
  235.  
  236. main (char argc, char **argv)
  237. {
  238.   char fname[80], oname[80];
  239.   char ascending=1;
  240.   char field=UID;
  241.   char switch_defined=0;
  242.   int i;
  243.  
  244.   strcpy(fname, "");
  245.   strcpy(oname, "");
  246.  
  247.   if (argc > 1)
  248.   {
  249.     for (i=1; i<argc; i++)
  250.     {
  251.       switch(argv[i][0])
  252.       {
  253.       case '-': switch(toupper(argv[i][1]))
  254.           {
  255.             case 'F' : if (strlen(argv[i]) > 2)
  256.                           strcpy(fname, &argv[i][2]);
  257.                         else if (argc < (i+2))
  258.                           haltusage();
  259.                         else
  260.                           strcpy(fname, argv[++i]);
  261.                         break;
  262.             case 'O' : if (strlen(argv[i]) > 2)
  263.                           strcpy(oname, &argv[i][2]);
  264.                         else if (argc < (i+2))
  265.                            haltusage();
  266.                         else
  267.                            strcpy(oname, argv[++i]);
  268.                         break;
  269.             case 'U' : if (switch_defined) haltusage();
  270.                        field=NAME;
  271.                        break;
  272.             case 'P' : if (switch_defined) haltusage();
  273.                        field=PASSWORD;
  274.                        break;
  275.             case 'I' : if (switch_defined) haltusage();
  276.                        field=UID;
  277.                        break;
  278.             case 'G' : if (switch_defined) haltusage();
  279.                        field=GID;
  280.                        break;
  281.             case 'N' : if (switch_defined) haltusage();
  282.                        field=GECOS;
  283.                        break;
  284.             case 'H' : if (switch_defined) haltusage();
  285.                        field=HOMEDIR;
  286.                        break;
  287.             case 'S' : if (switch_defined) haltusage();
  288.                        field=SHELL;
  289.                        break;
  290.             case 'R' : ascending = 0; break;
  291.             default  : haltusage();
  292.           }
  293.           break;
  294.       default : haltusage();
  295.       }
  296.     }
  297.   }
  298.  
  299.   if (strcmp(fname,""))
  300.     infile = fopen(fname, "rt");
  301.   else
  302.     infile = stdin;
  303.  
  304.   if (!infile)
  305.     {
  306.     fprintf(stderr, "sortpwd: %s: couldn't open file\n", fname);
  307.     exit(0);
  308.     }
  309.  
  310.   if (strcmp(oname, ""))
  311.     outfile = fopen(oname, "wt");
  312.   else
  313.     outfile = stdout;
  314.  
  315.   if (!outfile)
  316.     {
  317.     fprintf(stderr, "sortpwd: %s: could't create file\n", oname);
  318.     exit(0);
  319.     }
  320.  
  321.   do_sortpwd(infile, outfile, field, ascending);
  322.  
  323.   fclose(infile);
  324.   fclose(outfile);
  325.  
  326.   }
  327.  
  328. /* EOF SORTPWD.C */
  329.  
  330.